//<MStar Software>
//******************************************************************************
// MStar Software
// Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
// All software, firmware and related documentation herein ("MStar Software") are
// intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
// law, including, but not limited to, copyright law and international treaties.
// Any use, modification, reproduction, retransmission, or republication of all
// or part of MStar Software is expressly prohibited, unless prior written
// permission has been granted by MStar.
//
// By accessing, browsing and/or using MStar Software, you acknowledge that you
// have read, understood, and agree, to be bound by below terms ("Terms") and to
// comply with all applicable laws and regulations:
//
// 1. MStar shall retain any and all right, ownership and interest to MStar
//    Software and any modification/derivatives thereof.
//    No right, ownership, or interest to MStar Software and any
//    modification/derivatives thereof is transferred to you under Terms.
//
// 2. You understand that MStar Software might include, incorporate or be
//    supplied together with third party's software and the use of MStar
//    Software may require additional licenses from third parties.
//    Therefore, you hereby agree it is your sole responsibility to separately
//    obtain any and all third party right and license necessary for your use of
//    such third party's software.
//
// 3. MStar Software and any modification/derivatives thereof shall be deemed as
//    MStar's confidential information and you agree to keep MStar's
//    confidential information in strictest confidence and not disclose to any
//    third party.
//
// 4. MStar Software is provided on an "AS IS" basis without warranties of any
//    kind. Any warranties are hereby expressly disclaimed by MStar, including
//    without limitation, any warranties of merchantability, non-infringement of
//    intellectual property rights, fitness for a particular purpose, error free
//    and in conformity with any international standard.  You agree to waive any
//    claim against MStar for any loss, damage, cost or expense that you may
//    incur related to your use of MStar Software.
//    In no event shall MStar be liable for any direct, indirect, incidental or
//    consequential damages, including without limitation, lost of profit or
//    revenues, lost or damage of data, and unauthorized system use.
//    You agree that this Section 4 shall still apply without being affected
//    even if MStar Software has been modified by MStar in accordance with your
//    request or instruction for your use, except otherwise agreed by both
//    parties in writing.
//
// 5. If requested, MStar may from time to time provide technical supports or
//    services in relation with MStar Software to you for your use of
//    MStar Software in conjunction with your or your customer's product
//    ("Services").
//    You understand and agree that, except otherwise agreed by both parties in
//    writing, Services are provided on an "AS IS" basis and the warranty
//    disclaimer set forth in Section 4 above shall apply.
//
// 6. Nothing contained herein shall be construed as by implication, estoppels
//    or otherwise:
//    (a) conferring any license or right to use MStar name, trademark, service
//        mark, symbol or any other identification;
//    (b) obligating MStar or any of its affiliates to furnish any person,
//        including without limitation, you and your customers, any assistance
//        of any kind whatsoever, or any information; or
//    (c) conferring any license or right under any intellectual property right.
//
// 7. These terms shall be governed by and construed in accordance with the laws
//    of Taiwan, R.O.C., excluding its conflict of law rules.
//    Any and all dispute arising out hereof or related hereto shall be finally
//    settled by arbitration referred to the Chinese Arbitration Association,
//    Taipei in accordance with the ROC Arbitration Law and the Arbitration
//    Rules of the Association by three (3) arbitrators appointed in accordance
//    with the said Rules.
//    The place of arbitration shall be in Taipei, Taiwan and the language shall
//    be English.
//    The arbitration award shall be final and binding to both parties.
//
//******************************************************************************
//<MStar Software>

package com.mstar.android.media;

import android.os.Parcel;
import android.util.Log;

/**
 * Class to hold the subtitle's information. SubtitleTrackInfo are used for
 * human consumption and can be embedded in the media (e.g shoutcast) or
 * available from an external source. The source can be local (e.g thumbnail
 * stored in the DB) or remote (e.g caption server). NOTE:Users are only allowed
 * to use the getXXX function to get the information they need. Can't ues this
 * class alone.Make sure that you have called
 * getSubtitleTrackInfo/getAllSubtitleTrackInfo function in mediaplayer.java
 * before use these interfaces. eg: SubtitleTrackInfo info =
 * getAllSubtitleTrackInfo(); int totalInfoNum = info.getAllSubtitleCount(); int
 * allInternalSubtitleCount = info.getAllInternalSubtitleCount(); int
 * allImageSubtitleCount = info.getAllImageSubtitleCount(); int subtitleType[] =
 * new int[totalInfoNum]; info.getSubtitleType(subtitleType); int
 * subtitleCodeType[] = new int[totalInfoNum];
 * info.getSubtitleCodeType(subtitleCodeType); String subtitleLanguageType[] =
 * new String[totalInfoNum];
 * info.getSubtitleLanguageType(subtitleLanguageType,false);
 */
public class SubtitleTrackInfo {
    private final static String TAG = "SubtitleTrackInfo";

    // MAX_SUBTITLE_TRACK must be synchronized with MAX_SUBTITLE_TRACK in
    // mstarplayer.h
    private final int MAX_SUBTITLE_TRACK = 20;

    private final int INFO_NUM = 3;

    private enum SUBTITLE_LANGUAGE_TYPE {
        /// Invalid.
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_UNDEFINED,
        /// GERMAN
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_GERMAN,
        /// ENGLISH
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_ENGLISH,
        /// SPANISH
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_SPANISH,
        /// GREEK
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_GREEK,
        /// FRENCH
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_FRENCH,
        /// CROATIAN
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_CROATIAN,
        /// ITALIAN
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_ITALIAN,
        /// DUTCH
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_DUTCH,
        /// POLISH
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_POLISH,
        /// PORTUGUESE
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_PORTUGUESE,
        /// RUSSIAN
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_RUSSIAN,
        /// ROMANIAN
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_ROMANIAN,
        /// SWEDISH
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_SWEDISH,
        /// ARABIC
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_ARABIC,
        /// CHINESE
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_CHINESE,
        /// JAPANESE
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_JAPANESE,
        /// KOREAN
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_KOREAN,
        /// FARSI
        E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_FARSI,
        /// FINNISH
        E_MEDIA_SUBTITLE_PARSER_LANGUAGE_FINNISH,
        /// NORWEGIAN
        E_MEDIA_SUBTITLE_PARSER_LANGUAGE_NORWEGIAN,
        E_MEDIA_SUBTITLE_PARSER_LANGUAGE_HEBREW,
        E_MEDIA_SUBTITLE_PARSER_LANGUAGE_BULGARIAN,
        ///DANISH
        E_MEDIA_SUBTITLE_PARSER_LANGUAGE_DANISH,
        ///HINDI
        E_MEDIA_SUBTITLE_PARSER_LANGUAGE_HINDI,
        ///Counts
        E_MEDIA_SUBTITLE_PARSER_LANGUAGE_NUM,
    };

    private enum SUBTITLE_PARSER_CODE_TYPE {
        /// Unknown.
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_UNKNOWN,
        /// Unicode.
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_UNICODE,
        /// Unicode big endian.
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_UNICODE_BIG,
        /// UTF8.
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_UTF8,
        /// Native, such as BIG5, GB...
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_NATIVE,
        /// Bitmap format ARGB8888.
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_ARGB8888,
        /// Bitmap format ARGB1555.
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_ARGB1555,
        /// Bitmap format ARGB4444.
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_ARGB4444,
        /// Bitmap format YUV422.
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_YUV422,
        /// Bitmap format palette 2.
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_I2,
        /// Bitmap format palette 4.
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_I4,
        /// Bitmap format palette 8.
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_I8,
        /// the total counts of this enum
        E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_NUM,
        /// Invalid.
        // E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_INVALID = E_MEDIA_SUBTITLE_PARSER_CODE_TYPE_NUM,
    };

    private enum SUBTITLE_FILE_TYPE
    {
        /// SMI
        E_MEDIA_SUBTITLE_FILE_TYPE_SMI,
        /// SRT
        E_MEDIA_SUBTITLE_FILE_TYPE_SRT,
        /// SSA
        E_MEDIA_SUBTITLE_FILE_TYPE_SSA,
        /// ASS
        E_MEDIA_SUBTITLE_FILE_TYPE_ASS,
        /// SUB
        E_MEDIA_SUBTITLE_FILE_TYPE_SUB,
        /// TXT
        E_MEDIA_SUBTITLE_FILE_TYPE_TXT,
        /// SUB & IDX
        E_MEDIA_SUBTITLE_FILE_TYPE_SUBIDX,
        /// SUP
        E_MEDIA_SUBTITLE_FILE_TYPE_SUP,
        /// Internal subtitle
        E_MEDIA_SUBTITLE_FILE_TYPE_INTERNAL,
        /// LRC
        E_MEDIA_SUBTITLE_FILE_TYPE_LRC,
        /// the total counts of this enum
        E_MEDIA_SUBTITLE_FILE_TYPE_NUM,
        /// Invalid
        //E_MEDIA_SUBTITLE_FILE_TYPE_INVALID = E_MEDIA_SUBTITLE_FILE_TYPE_NUM,
    };

    private SUBTITLE_LANGUAGE_TYPE mSubtitleLanguageType;

    private int mSubtitleNum;

    private int mDataPos;

    private boolean mIsGetAllInfo;

    private int[] mSubtitleType;

    private int[] mSubtitleCodeType;

    private int[] mSubtitleLanguageStyle;

    private int   mAllInternalSubtitleCount;

    private int   mAllImageSubtitleCount;

    private boolean mIsreplyNull = false;

    public SubtitleTrackInfo(Parcel reply) {
        if (reply == null) {
            mIsreplyNull = true;
            Log.e(TAG, "The parcel getted is null "
                    + "OR calling SubtitleTrackInfo funtion with a wrong way!");
            return;
        }

        mSubtitleType = new int[1];
        mSubtitleCodeType = new int[1];
        mSubtitleLanguageStyle = new int[1];
        mIsGetAllInfo = false;
        mDataPos = reply.dataPosition();
        reply.setDataPosition(mDataPos);
        mSubtitleType[0] = reply.readInt();
        mSubtitleCodeType[0] = reply.readInt();
        mSubtitleLanguageStyle[0] = reply.readInt();
        Log.e(TAG, "mDataPos = " + mDataPos);
    };

    public SubtitleTrackInfo(Parcel reply, int subtitleNum) {
        if (reply == null) {
            mIsreplyNull = true;
            Log.e(TAG, "The parcel getted is null "
                    + "OR calling SubtitleTrackInfo funtion with a wrong way!");
            return;
        }

        int totalInfoNum = reply.readInt();
        mDataPos = reply.dataPosition();
        reply.setDataPosition(mDataPos);
        mIsGetAllInfo = true;
        mSubtitleNum = (totalInfoNum - 2) / INFO_NUM;

        mSubtitleType = new int[mSubtitleNum];
        mSubtitleCodeType = new int[mSubtitleNum];
        mSubtitleLanguageStyle = new int[mSubtitleNum];
        for(int i = 0; i < mSubtitleNum; i++) {
            mSubtitleType[i] = reply.readInt();
            mSubtitleCodeType[i] = reply.readInt();
            mSubtitleLanguageStyle[i] = reply.readInt();
        }
        mAllInternalSubtitleCount = reply.readInt();
        mAllImageSubtitleCount = reply.readInt();
        if (mSubtitleNum > MAX_SUBTITLE_TRACK) {
            Log.d(TAG, "Subtitle's number overflow! ");
            mSubtitleNum = MAX_SUBTITLE_TRACK;
        }

        if (totalInfoNum < 2) {
            Log.d(TAG, "The video is NOT playing through MstarPlayer or MstPlayer,"
                    + "so all the subtitle trakc infomations are invalid!");
        }
    };

    public int getSubtitleType() {
        if (mIsreplyNull == true) {
            Log.e(TAG, "The parcel in SubtitleTrackInfo object is null!");
            return -2;
        }

        return /*(SUBTITLE_FILE_TYPE)*/mSubtitleType[0];
    }

    public int getSubtitleCodeType() {
        if (mIsreplyNull == true) {
            Log.e(TAG, "The parcel in SubtitleTrackInfo object is null!");
            return -2;
        }

        return /*(SUBTITLE_PARSER_CODE_TYPE)*/mSubtitleCodeType[0];
    }

    public String getSubtitleLanguageType(boolean isChinese) {
        if (mIsreplyNull == true) {
            Log.e(TAG, "The parcel in SubtitleTrackInfo object is null!");
            return null;
        }

        if (mIsGetAllInfo == false) {
            mSubtitleLanguageType = SUBTITLE_LANGUAGE_TYPE.values()[mSubtitleLanguageStyle[0]];
        }

        if (mSubtitleLanguageType == null) {
            return null;
        }
        String subtitleLanguageType;
        if (!isChinese) {
            switch (mSubtitleLanguageType) {
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_GERMAN:
                    subtitleLanguageType = "German";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_ENGLISH:
                    subtitleLanguageType = "English";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_SPANISH:
                    subtitleLanguageType = "Spanish";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_GREEK:
                    subtitleLanguageType = "Greek";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_FRENCH:
                    subtitleLanguageType = "French";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_CROATIAN:
                    subtitleLanguageType = "Croatian";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_ITALIAN:
                    subtitleLanguageType = "Italian";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_DUTCH:
                    subtitleLanguageType = "Dutch";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_POLISH:
                    subtitleLanguageType = "Polish";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_PORTUGUESE:
                    subtitleLanguageType = "Portuguese";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_RUSSIAN:
                    subtitleLanguageType = "Russian";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_ROMANIAN:
                    subtitleLanguageType = "Romanian";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_SWEDISH:
                    subtitleLanguageType = "Swedish";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_ARABIC:
                    subtitleLanguageType = "Arabic";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_CHINESE:
                    subtitleLanguageType = "Chinese";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_JAPANESE:
                    subtitleLanguageType = "Japanese";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_KOREAN:
                    subtitleLanguageType = "Korean";
                    break;
                case E_MS_PLAYER_SUBTITLE_PARSER_LANGUAGE_FARSI:
                    subtitleLanguageType = "Farsi";
                    break;
                case E_MEDIA_SUBTITLE_PARSER_LANGUAGE_FINNISH:
                    subtitleLanguageType = "Finnish";
                    break;
                case E_MEDIA_SUBTITLE_PARSER_LANGUAGE_NORWEGIAN:
                    subtitleLanguageType = "Norwegian";
                    break;
                case E_MEDIA_SUBTITLE_PARSER_LANGUAGE_HEBREW:
                    subtitleLanguageType = "Hebrew";
                    break;
                case E_MEDIA_SUBTITLE_PARSER_LANGUAGE_BULGARIAN:
                    subtitleLanguageType = "Bulgarian";
                    break;
                case E_MEDIA_SUBTITLE_PARSER_LANGUAGE_DANISH:
                    subtitleLanguageType = "Danish";
                    break;
                case E_MEDIA_SUBTITLE_PARSER_LANGUAGE_HINDI:
                    subtitleLanguageType = "Hindi";
                    break;
                default:
                    subtitleLanguageType = "undefined";
                    break;
            }
        } else {
            subtitleLanguageType = "";
        }

        return subtitleLanguageType;
    }

    public void getSubtitleType(int[] info) {
        if (mIsreplyNull == true) {
            Log.e(TAG, "The parcel in SubtitleTrackInfo object is null!");
            return;
        }

        for (int i = 0; i < mSubtitleNum; i++) {
            try {
                info[i] = /*(SUBTITLE_FILE_TYPE)*/mSubtitleType[i];
            } catch (ArrayIndexOutOfBoundsException ex) {
                Log.e(TAG, "Parameter of getSubtitleType function must be allocated"
                        + " based on the return value of getAllSubtitleCount()!", ex);
                break;
            }
        }
    }

    public void getSubtitleCodeType(int[] info) {
        if (mIsreplyNull == true) {
            Log.e(TAG, "The parcel in SubtitleTrackInfo object is null!");
            return;
        }

        for (int i = 0; i < mSubtitleNum; i++) {
            try {
                info[i] = /*(SUBTITLE_PARSER_CODE_TYPE)*/mSubtitleCodeType[i];
            } catch (ArrayIndexOutOfBoundsException ex) {
                Log.e(TAG, "Parameter of getSubtitleCodeType function must be allocated"
                        + " based on the return value of getAllSubtitleCount()!", ex);
                break;
            }
        }
    }

    public void getSubtitleLanguageType(String[] info, boolean isChinese) {
        if (mIsreplyNull == true) {
            Log.e(TAG, "The parcel in SubtitleTrackInfo object is null!");
            return;
        }

        for (int i = 0; i < mSubtitleNum; i++) {
            mSubtitleLanguageType = SUBTITLE_LANGUAGE_TYPE.values()[mSubtitleLanguageStyle[i]];
            try {
                info[i] = getSubtitleLanguageType(isChinese);
            } catch (ArrayIndexOutOfBoundsException ex) {
                Log.e(TAG, "Parameter of getSubtitleLanguageType function must be allocated"
                        + " based on the return value of getAllSubtitleCount()!", ex);
                break;
            }
        }
    }

    public int getAllSubtitleCount() {
        return mSubtitleNum;
    }

    public int getAllInternalSubtitleCount() {
        if (mIsreplyNull == true) {
            Log.e(TAG, "The parcel in SubtitleTrackInfo object is null!");
            return -2;
        }

        return mAllInternalSubtitleCount;
    }

    public int getAllImageSubtitleCount() {
        if (mIsreplyNull == true) {
            Log.e(TAG, "The parcel in SubtitleTrackInfo object is null!");
            return -2;
        }

        return mAllImageSubtitleCount;
    }

}
